import CryptoJS from "crypto-js";

/**
 * 解析音频数据
 * @param {File} file 文件对象
 * @returns {Promise<{duration:number}>} 返回解析出来的音频信息
 */
const analysisAudioObj = new Audio();
export function analysisAudio(file) {
    const filePath = URL.createObjectURL(file);
    analysisAudioObj.src = filePath;
    return new Promise((resolve) => {
        analysisAudioObj.onloadedmetadata = () => {
            const returnBody = {
                duration: Math.ceil(analysisAudioObj.duration),
            };
            resolve(returnBody);
            analysisAudioObj.src = "";
            URL.revokeObjectURL(filePath);// 销毁blob
        };
    });
}

export function computedSign(file) {
    const suffix = file.name.slice(file.name.lastIndexOf(".") + 1); // 切出文件后缀名
    const encode = CryptoJS.MD5(`${Date.now()}-${file.name}-${Math.random()}`);
    const sign = `${Date.now()}-${encode}.${suffix}`; // 1603693924143-f53294a616ba34755756c7fc293af8f0bde741a138f7b1d9bc1aef7687a83c60.mp3
    return sign;
}
/**
* 验证音频文件格式
* @param {File} file 文件对象
* @returns {boolean} 验证结果
*/
export function validateAudioFileMime(file) {
    const acceptMimeTyep = [
        "audio/mpeg",
        "audio/mp3",
        "audio/ogg",
        "audio/wav",
    ];
    return acceptMimeTyep.includes(file.type);
}